package com.readen.leetcode;

/**
 * 
 * add two binary string
 * 
 * @author Administrator
 *
 */
public class AddBinary {

	
	 public String addBinary(String a, String b) {
	        int lenA=a.length();
	        int lenB=b.length();
	        int maxLen=Math.max(lenA,lenB);
	        StringBuffer buffer=new StringBuffer();
	        char c='0';
	        for(int i=0;i<maxLen;i++){
	            char ca='0',cb='0';
	            int pa=lenA-i-1,pb=lenB-i-1;
	            if(pa>=0){
	                ca=a.charAt(pa);
	            }
	            if(pb>=0){
	                cb=b.charAt(pb);
	            }
	            if(ca==cb){
	                buffer.append(c);
	                c=ca;
	            }else{
	                if(c=='1'){
	                    buffer.append('0');
	                }else{
	                    buffer.append('1');
	                    c='0';
	                }
	            }
	        }
	       if(c=='1'){
	           buffer.append(c);
	       } 
	       return buffer.reverse().toString();
	    }
	
}
